ml-spectra-fitting
This is a spectra fitting package to optimize the position (x), max intensity (y), full width at half maximum (FWHM = width) and the ratio of gaussian contribution (mu) if it's required. It supports three kind of shapes:
Name | Equation |
---|
Gaussian | |
Lorentzian | |
Pseudo Voigt | |
where
It is a wrapper of ml-levenberg-marquardt
Installation
$ npm install ml-spectra-fitting
Example
import { optimizeSum } from 'ml-spectra-fitting';
import { generateSpectrum } from 'spectrum-generator';
const peaks = [
{ x: 0.5, y: 0.2, width: 0.2 },
{ x: -0.5, y: 0.2, width: 0.3 },
];
const data = generateSpectrum(peaks, { from: -1, to: 1, nbPoints: 41 });
let peaks = [
{
x: -0.5,
y: 0.18,
width: 0.18,
},
{
x: 0.52,
y: 0.17,
width: 0.37,
},
];
let fittedPeaks = optimize(data, peaks);
console.log(fittedPeaks);
For data with and combination of signals with shapes between gaussian and lorentzians, we could use the kind pseudovoigt to fit the data.
import { optimize } from 'ml-spectra-fitting';
import { SpectrumGenerator } from 'spectrum-generator';
const generator = new SpectrumGenerator({
nbPoints: 101,
from: -1,
to: 1,
});
generator.addPeak({ x: 0.5, y: 0.2 }, { width: 0.2 });
generator.addPeak(
{ x: -0.5, y: 0.2 },
{
width: 0.1,
shape: {
kind: 'lorentzian',
options: {
fwhm: 1000,
length: 50001,
factor: 5,
},
},
},
);
let data = generator.getSpectrum();
console.log(JSON.stringify({ x: Array.from(data.x), y: Array.from(data.y) }));
let peaks = [
{
x: -0.5,
y: 0.22,
width: 0.25,
},
{
x: 0.52,
y: 0.18,
width: 0.18,
},
];
let fittedParams = optimize(data, peaks, { shape: { kind: 'pseudovoigt' } });
console.log(fittedParams);
License
MIT